/* eslint-disable no-unreachable */
<template>
  <alert :alertData="alertData" :opacity="alertOpacity" />

  <div class="div-content-wrapper d-flex justify-content-center">
    <!-- 疾病字段输入框容器 -->
    <div class="div-input-wrapper">
      <div class="d-flex flex-column justify-content-center">
        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-person-half-dress"></i>
            患者性别
          </span>
          <span class="span-field-name-slope" />
        </div>

        <div class="div-jbzd-gender d-flex flex-row mb-4">
          <div class="form-check me-5">
            <input
              class="form-check-input"
              type="radio"
              value="0"
              v-model.number="gender"
            />
            <label @click="this.gender = 0">男</label>
          </div>

          <div class="form-check">
            <input
              class="form-check-input"
              type="radio"
              value="1"
              v-model.number="gender"
            />
            <label @click="this.gender = 1">女</label>
          </div>
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-user-nurse"></i>
            患者年龄
          </span>
          <span class="span-field-name-slope" />
        </div>

        <div class="input-group mb-4">
          <input
            class="in-jbzd-age form-control"
            type="number"
            min="0"
            max="120"
            v-model.number="age"
          />
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-file-medical"></i>
            主诉
          </span>
          <span class="span-field-name-slope" />
        </div>
        <div class="input-group mb-3">
          <textarea
            class="ta-jbzd-field form-control"
            v-model="chiefComplaint"
          />
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-file-medical"></i>
            现病史
          </span>
          <span class="span-field-name-slope" />
        </div>
        <div class="input-group mb-3">
          <textarea
            class="ta-jbzd-field form-control"
            v-model="presentHistory"
          />
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-file-medical"></i>
            既往史
          </span>
          <span class="span-field-name-slope" />
        </div>
        <div class="input-group mb-3">
          <textarea class="ta-jbzd-field form-control" v-model="pastHistory" />
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-file-medical"></i>
            体格检查
          </span>
          <span class="span-field-name-slope" />
        </div>
        <div class="input-group mb-3">
          <textarea class="ta-jbzd-field form-control" v-model="physical" />
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-file-medical"></i>
            辅助检查
          </span>
          <span class="span-field-name-slope" />
        </div>
        <div class="input-group mb-3">
          <textarea
            class="ta-jbzd-field form-control"
            v-model="supplementary"
          />
        </div>

        <div class="div-field-name-wrapper">
          <span class="span-field-name">
            <i class="i-field-icon fa-solid fa-file-medical"></i>
            专科检查
          </span>
          <span class="span-field-name-slope" />
        </div>
        <div class="input-group mb-4">
          <textarea class="ta-jbzd-field form-control" v-model="speciality" />
        </div>

        <button
          class="btn btn-primary"
          style="height: 50px"
          @click="submitDiagnosis"
        >
          <span
            class="spinner-border spinner-border-sm"
            v-show="isLoadingShow"
          />
          AI在线诊断
        </button>
      </div>
    </div>

    <!-- 诊断结果容器暗色墙 -->
    <div
      class="div-result-wall"
      v-show="isDiagnosisResultShow"
      :style="{ opacity: diagnosisResultOpacity }"
      @click="onResultWallClick"
    >
      <!-- 诊断结果容器 -->
      <div class="div-result-wrapper" @click.stop="">
        <i class="fa-solid fa-user-doctor"></i>
        <label class="ms-3">{{ diagnosisTitle }}</label>

        <div class="d-flex flex-column mt-2">
          <label class="mb-3">可能与您的描述相关联的病症：</label>
          <label v-if="diagnosisResult.length === 0">暂无结果</label>

          <div class="d-flex flex-row flex-wrap">
            <single-disease
              class="me-4 mb-3"
              v-for="(item, index) in diagnosisResult"
              :key="index"
              :dataset="item"
              @click="showDiagnosisDetail(index)"
            ></single-disease>
          </div>
        </div>
      </div>
    </div>
  </div>

  <hwmi-dialog
    title="诊断详情"
    :show="isDialogShow"
    :style="{ opacity: dialogOpacity, width: dialogWidth }"
    @close="closeDiagnosisDetail"
  >
    <div class="d-flex flex-column p-2">
      <label style="margin-top: 15px">
        诊断疾病：{{ diagnosisResult[selectedDiagnosisResultIndex].disease }}
      </label>

      <div class="div-split-line"></div>

      <label
        >诊断模型：{{
          diagnosisResult[selectedDiagnosisResultIndex].source
        }}</label
      >

      <div class="div-split-line"></div>

      <label>
        置信度：{{
          diagnosisResult[selectedDiagnosisResultIndex].confidence >= 0
            ? (
                diagnosisResult[selectedDiagnosisResultIndex].confidence * 100
              ).toFixed(2) + "%"
            : "暂无置信度"
        }}
      </label>

      <div class="div-split-line"></div>

      <div
        class="d-flex flex-row justify-content-around"
        style="margin-bottom: 15px"
      >
        <div
          class="d-flex align-items-center"
          style="cursor: pointer"
          @click="goToZstp"
        >
          <i
            class="fa-solid fa-circle-nodes fa-2x"
            style="color: royalblue"
          ></i>
          <span style="margin-left: 10px">知识图谱</span>
        </div>

        <div
          class="d-flex align-items-center"
          style="cursor: pointer"
          @click="goToPedia"
        >
          <i class="fa-solid fa-book-atlas fa-2x" style="color: royalblue"></i>
          <span style="margin-left: 10px">在线百科</span>
        </div>
      </div>
    </div>
  </hwmi-dialog>
</template>

<script>
import axios from "axios";
import ANIMATIONS_COMMON from "../modules/AnimationsCommon";
import SingleDisease from "./jbzd/single-disease.vue";
import HwmiDialog from "./Common/hwmi-dialog.vue";
import { HWMI_URLS } from "../modules/Api";
import ANIMATIONS from "@/modules/jbzd/Animations";
import { isMobileBrowser } from "@/modules/Utilities";

import Alert from "./Common/alert.vue";

export default {
  components: {
    Alert,
    SingleDisease,
    HwmiDialog,
  },
  data: () => ({
    alertData: {
      show: false,
      message: "",
      type: "primary",
    },
    alertOpacity: 0,

    isLoadingShow: false,

    gender: 0,
    age: 20,
    chiefComplaint: "",
    presentHistory: "",
    pastHistory: "",
    physical: "",
    supplementary: "",
    speciality: "",

    isDiagnosisResultShow: false,
    diagnosisResultOpacity: 0,

    diagnosisTitle: "轻点按钮，体验AI疾病诊断",
    diagnosisResult: [{}],
    selectedDiagnosisResultIndex: 0,

    isDialogShow: false,
    dialogOpacity: 0,
    dialogWidth: isMobileBrowser() ? "80%" : "60%",
  }),
  watch: {
    age(newVal) {
      if (newVal < 0) {
        this.age = 0;
      }

      if (newVal > 120) {
        this.age = 120;
      }
    },
  },
  methods: {
    submitDiagnosis() {
      if (this.isLoadingShow || this.alertData.show) {
        return;
      }

      const requestData = {
        gender: this.gender,
        age: this.age,
        chiefComplaint: this.chiefComplaint,
        presentHistory: this.presentHistory,
        pastHistory: this.pastHistory,
        physical: this.physical,
        supplementary: this.supplementary,
        speciality: this.speciality,
      };

      for (const i in requestData) {
        if (requestData[i] === "") {
          ANIMATIONS_COMMON.showAlert(this, "请您填全所有信息", "danger");
          return;
        }
      }

      this.isLoadingShow = true;

      const requestJsonString = JSON.stringify(requestData);

      axios
        .post(HWMI_URLS.jbzdModel1RequestUrl, requestJsonString)
        .then((res) => {
          axios
            .post(HWMI_URLS.jbzdModel2RequestUrl, requestJsonString)
            .then((res1) => {
              this.showDiagnosisResult([res.data.data, res1.data.data]);
            })
            .catch((reason) => {
              console.log(reason);
              ANIMATIONS_COMMON.showAlert(this, reason, "danger");
            })
            .finally(() => {
              this.isLoadingShow = false;
            });
        })
        .catch((reason) => {
          console.log(reason);
          ANIMATIONS_COMMON.showAlert(this, reason, "danger");
          this.isLoadingShow = false;
        });
    },
    showDiagnosisResult(data) {
      this.diagnosisTitle = "诊断结果（仅供参考）：";

      let diagnosisResult = [];
      for (const i of data) {
        diagnosisResult = diagnosisResult.concat(i);
      }

      // how to define a correct compare function:
      // any two sequencing sorted element passed as a, b can return a negative value.
      diagnosisResult.sort((a, b) => b.confidence - a.confidence);

      this.diagnosisResult = diagnosisResult;

      ANIMATIONS.showDiagnosisResult(this);
    },
    onResultWallClick() {
      ANIMATIONS.hideDiagnosisResult(this);
    },
    showDiagnosisDetail(index) {
      this.selectedDiagnosisResultIndex = index;
      ANIMATIONS.showDiagnosisDetail(this);
    },
    closeDiagnosisDetail() {
      ANIMATIONS.closeDiagnosisDetail(this);
    },
    goToZstp() {
      this.$router.push({
        name: "zstp",
        query: {
          keyword:
            this.diagnosisResult[this.selectedDiagnosisResultIndex].disease+"..疾病",
        },
      });
    },
    goToPedia() {
      window.open(
        `https://baike.baidu.com/item/${
          this.diagnosisResult[this.selectedDiagnosisResultIndex].disease
        }`
      );
    },
  },
};
</script>

<!--Add scoped or styles with same name will affect other components.-->
<style lang="scss" scoped>
@import "../CommonStyles";
@import "../App";

$field-background-color: rgba(255, 255, 255, 0.5);
$field-name-width: 7em;

/* Background color must be used or brightness will have no effect on background. */
.div-content-wrapper {
  @include full-parent-size;
  @include page-background-image("../assets/images/jbzd-bkg.png");
}

.div-input-wrapper {
  width: m#{i}n(800px, 100%); // use this to override default SCSS min function
  height: 100%;
  padding: 20px;
  overflow: auto;

  @include slim-scroll-bar;
}

.div-field-name-wrapper {
  position: relative;
  font-size: 15px;
  line-height: 1em;
}

.i-field-icon {
  color: rgba(0,0,0,0.8);
  width: 1em;
  margin-right: 0.5em;
}

.span-field-name {
  display: inline-block;
  width: $field-name-width;
  margin: 0;
  padding: 8px;
  border: 1px solid #ced4da;
  border-right: 0;
  border-bottom: 0;
  border-top-left-radius: 0.5rem;

  background-color: $field-background-color;
}

.span-field-name-slope {
  display: inline-block;
  position: absolute;
  width: 6em;
  height: 32.5px;
  bottom: 0;
  left: $field-name-width;

  background-color: transparent;

  overflow: hidden;

  &::after {
    content: "";
    position: absolute;

    top: 15.3px;
    left: -18px; // -31px*sin(x)

    width: 70px; // >31px/sin(x)
    height: 31px;
    border-top: 1px solid #ced4da;
    background-color: $field-background-color;

    transform: rotate(35deg);
  }
}

@mixin jbzd-field {
  border: 1px solid #ced4da;
  border-radius: 0.5rem;
  border-top-left-radius: 0;
  background-color: $field-background-color;
}

.div-jbzd-gender {
  padding: 0.375rem 0.75rem;
  @include jbzd-field;
}

.in-jbzd-age {
  @include jbzd-field;
}

.ta-jbzd-field {
  @include jbzd-field;
  height: 100px;
}

.div-result-wall {
  position: fixed;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);

  z-index: $z-index-jbzd-result-wall;
}

.div-result-wrapper {
  position: fixed;
  width: m#{i}n(600px, 90%);
  max-height: 85%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  border-radius: 5px;
  background: linear-gradient(to right, rgb(0, 207, 166), rgb(0, 211, 231));
  box-shadow: 1px 1px 10px 0px rgba(0, 0, 0, 0.28);
  overflow: auto;

  @include slim-scroll-bar;
  z-index: $z-index-jbzd-result-wrapper;
}
</style>